# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from typing import List
from exception import AppException
from common.utils.times import TimeUtil
from common.models.draws import DrawsPromptCategoryModel
from apps.admin.schemas.apply.draws import prompt_category_schema as schema


class DrawsPromptCategoryService:
    """ 绘画提示词分类服务类 """

    @classmethod
    async def lists(cls, params: schema.DrawsPromptCategorySearchIn) -> List[schema.DrawsPromptCategoryVo]:
        """
        提示词分类列表

        Args:
            params (schema.DrawsPromptCategorySearchIn): 搜索参数

        Returns:
            List[schema.DrawsPromptCategoryVo]

        Author:
            zero
        """
        where = DrawsPromptCategoryModel.build_search({
            "=": ["is_disable"],
            "%like%": ["name"],
            "datetime": ["start_time|end_time@create_time"]
        }, params.__dict__)

        lists = await (DrawsPromptCategoryModel
                       .filter(engine=params.engine)
                       .filter(is_delete=0)
                       .filter(*where)
                       .order_by("-sort", "-id")
                       .all())

        _lists = []
        for item in lists:
            _lists.append(schema.DrawsPromptCategoryVo(
                id=item.id,
                engine=item.engine,
                name=item.name,
                sort=item.sort,
                is_disable=item.is_disable,
                create_time=TimeUtil.timestamp_to_date(item.create_time)

            ))
        return _lists

    @classmethod
    async def add(cls, post: schema.DrawsPromptCategoryAddIn) -> None:
        """
        提示词分类新增

        Args:
            post (schema.DrawsPromptCategoryAddIn): 新增参数

        Author:
            zero
        """
        category = await (DrawsPromptCategoryModel
                          .filter(engine=post.engine)
                          .filter(is_delete=0)
                          .filter(name=post.name)
                          .first())

        if category:
            raise AppException("分类名称已存在,换一个吧")

        await DrawsPromptCategoryModel.create(
            engine=post.engine,
            name=post.name,
            sort=post.sort,
            is_disable=post.is_disable,
            create_time=int(time.time()),
            update_time=int(time.time())
        )

    @classmethod
    async def edit(cls, post: schema.DrawsPromptCategoryEditIn) -> None:
        """
        提示词分类编辑

        Args:
            post (schema.DrawsPromptCategoryAddIn): 编辑参数

        Author:
            zero
        """
        await DrawsPromptCategoryModel.filter(id=post.id).get()
        category = await (DrawsPromptCategoryModel
                          .filter(engine=post.engine)
                          .filter(is_delete=0)
                          .filter(id__not=post.id)
                          .filter(name=post.name)
                          .first())

        if category:
            raise AppException("分类名称已存在,换一个吧")

        await DrawsPromptCategoryModel.filter(id=post.id).update(
            engine=post.engine,
            name=post.name,
            sort=post.sort,
            is_disable=post.is_disable,
            update_time=int(time.time())
        )

    @classmethod
    async def delete(cls, id_: int, engine: str) -> None:
        """
        提示词分类删除

        Args:
            id_ (int): 分类ID
            engine (int): 绘图渠道

        Author:
            zero
        """
        await DrawsPromptCategoryModel.filter(id=id_, engine=engine).get()
        await DrawsPromptCategoryModel.filter(id=id_).update(
            is_delete=1,
            delete_time=int(time.time())
        )
